package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.api.model.ProvinceDTO;
import com.mhc.suzuki.api.service.IProvinceService;
import com.mhc.suzuki.bo.ProvinceBO;
import com.mhc.suzuki.constant.IsDeletedEnum;
import com.mhc.suzuki.dal.mapper.ext.ProvinceExtMapper;
import com.mhc.suzuki.dal.model.ProvinceDO;
import com.mhc.suzuki.exception.SuzukiCommonException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HMJ on 2017/10/17.
 */
@Slf4j
@RestController
@Transactional(rollbackFor = Exception.class)
public class ProvinceServiceImpl implements IProvinceService {

    @Autowired
    private ProvinceExtMapper provinceExtMapper;

    /**
     * 查询省份列表
     * @param provinceBO
     * @return
     */
    @Override
    public List<ProvinceDTO> getProvinceList (ProvinceBO provinceBO) throws SuzukiCommonException{
        log.info("正在查询省份列表...");
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("keyword", provinceBO.getKeyword());           //关键字
        paramMap.put("isDeleted", IsDeletedEnum.NORMAL.getCode());     //未删除
        List<ProvinceDO> provinceList = provinceExtMapper.selectByProvinceList(paramMap);
        List<ProvinceDTO> provinceDTOList = new ArrayList<>();
        for(ProvinceDO provinceDO : provinceList){
            ProvinceDTO provinceDTO = new ProvinceDTO();
            BeanUtils.copyProperties(provinceDO, provinceDTO);
            provinceDTOList.add(provinceDTO);
        }
        log.info("查询完成{}",provinceList);
        return provinceDTOList;
    }


}
